package com.wits.qso.dao.impl;

import java.util.ArrayList;
import java.util.List;

import javax.persistence.Query;

import org.springframework.stereotype.Component;

import com.wits.platform.dao.impl.BaseDaoImpl;
import com.wits.qso.dao.custom.AttachmentDaoCustom;
import com.wits.qso.entity.Attachment;

@Component
public class AttachmentDaoImpl extends BaseDaoImpl<Attachment, String> implements AttachmentDaoCustom{

    public int deleteByBusinessId(String businessId) {
    	Query query = em.createQuery( "DELETE FROM Attachment o WHERE o.businessId = :businessId" );
    	query.setParameter( "businessId", businessId );
    	
    	return query.executeUpdate();
    }

	@Override
	public List<Attachment> bulkSave(List<Attachment> entities) {
		List<Attachment> list=new ArrayList<Attachment>();
		for(Attachment entity:entities){
			em.flush();
			em.persist(entity);
			list.add(entity);
		}
		return list;
	}

	@Override
	public int bulkUpdate(List<Attachment> entities) {
		int count = 0;  //统计受影响的行数
		if(null == entities || entities.size() == 0){
			return count;
		}
		for(Attachment entity:entities){
			em.flush();
			Attachment t=em.merge(entity);
			count++;
		}
		return count;
	}

	@Override
	public List<Attachment> findByBusinessIdAndBusinessType(String businessId,
			String businessType) {
		Query query = em.createQuery( " FROM Attachment o WHERE o.businessId = :businessId AND o.businessType=:businessType" );
    	query.setParameter( "businessId", businessId );
    	query.setParameter( "businessType", businessType );
		return query.getResultList();
	}

	@Override
	public int deleteById(String id) {
		Query query = em.createQuery( "DELETE FROM Attachment o WHERE o.id = :id" );
    	query.setParameter( "id", id );
    	
    	return query.executeUpdate();
	}

}
